//
//  LCZNewCollectionCell.m
//  PersonalNews
//
//  Created by dllo on 15/11/27.
//  Copyright © 2015年 changshuhua. All rights reserved.
//

#import "LCZNewCollectionCell.h"
#import "LCZSpecialTableViewCell.h"
#import "LCZNormalTableViewCell.h"
#import "LCZPhotosetTableViewCell.h"
#import "LCZNewsModel.h"


#define pCOUNT self.picArr.count

@implementation LCZNewCollectionCell

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createTableView];
        [self createHeadView];
        [self createScrollView];
        

    }
    return self;
}

- (void)setPicArr:(NSMutableArray *)picArr {
    
//#error imageView随着picArrdset方法无限创建
    
    for (LCZBaseImageView *temp in self.scrollView.subviews) {
        [temp removeFromSuperview];
    }
    if (_picArr != picArr) {
        _picArr = picArr;
        
        // 轮播图
        LCZBaseImageView *firstView = [[LCZBaseImageView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT / 3 - 30)];
        NSInteger count = self.picArr.count;
        [firstView sd_setImageWithURL:[NSURL URLWithString:[self.picArr[count - 1] imgsrc]]];
//        firstView.backgroundColor = [UIColor magentaColor];
        [self.scrollView addSubview:firstView];
        
        LCZBaseLabel *firstLabel = [[LCZBaseLabel alloc] initWithFrame:CGRectMake(0, self.headView.frame.size.height - 30, SCREENWIDTH, 30)];
//        firstLabel.backgroundColor = [UIColor brownColor];
        [self.scrollView addSubview:firstLabel];
        firstLabel.text = [self.picArr[count - 1] title];
        
        
        for (NSInteger i = 0; i < self.picArr.count; i++) {
            self.imageView = [[LCZBaseImageView alloc] initWithFrame:CGRectMake((i + 1)* SCREENWIDTH, 0, SCREENWIDTH , SCREENHEIGHT / 3 - 30)];
//            self.imageView.backgroundColor = [UIColor yellowColor];
            [self.scrollView addSubview:self.imageView];
            self.imageView.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
            
            [self.imageView addGestureRecognizer:tap];
            
            [self.imageView sd_setImageWithURL:[NSURL URLWithString:[self.picArr[i] imgsrc]] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"]];
          
            self.imageView.userInteractionEnabled = YES;
            //            [self.imageView addGesture];
            
            self.titleLabel = [[LCZBaseLabel alloc] initWithFrame:CGRectMake((i + 1) * SCREENWIDTH, self.headView.frame.size.height - 30, SCREENWIDTH, 30)];
//            self.titleLabel.backgroundColor = [UIColor brownColor];
            [self.scrollView addSubview:self.titleLabel];
            
            self.titleLabel.text = [self.picArr[i] title];
            
            self.titleLabel.nightTextColor = [UIColor grayColor];
            self.titleLabel.nightBackgroundColor = [UIColor blackColor];
            
            
        }
        
        LCZBaseImageView *lastView = [[LCZBaseImageView alloc] initWithFrame:CGRectMake((count + 1) * SCREENWIDTH, 0, SCREENWIDTH , SCREENHEIGHT / 3 - 30)];
        [lastView sd_setImageWithURL:[NSURL URLWithString:[self.picArr[0] imgsrc]]];
        [self.scrollView addSubview:lastView];
//        lastView.backgroundColor = [UIColor blackColor];
        
        LCZBaseLabel *lastLabel = [[LCZBaseLabel alloc] initWithFrame:CGRectMake((count + 1) * SCREENWIDTH, self.headView.frame.size.height - 30, SCREENWIDTH, 30)];
//        lastLabel.backgroundColor = [UIColor brownColor];
        [self.scrollView addSubview:lastLabel];
        lastLabel.text = [self.picArr[0] title];
        
        
        
        self.scrollView.contentOffset = CGPointMake(SCREENWIDTH, 0);
        self.scrollView.contentSize = CGSizeMake((self.picArr.count + 2) * SCREENWIDTH, 0);
        self.scrollView.showsHorizontalScrollIndicator = NO;
        
        
    }
    
    
    
}

- (void)tapAction:(UITapGestureRecognizer *)tap {
    NSLog(@"触摸点击");

    
//    if ([[self.newsArr[indexPath.row] skipType] isEqualToString:@"photoset"]) {

//    } else if ([[self.newsArr[indexPath.row] skipType] isEqualToString:@"special"]) {


    NSLog(@"%ld", (NSInteger)(self.scrollView.contentOffset.x / SCREENWIDTH));
    
    LCZNewsModel *model = self.newsArr[(NSInteger)(self.scrollView.contentOffset.x / SCREENWIDTH) ];
    if ([model.skipType isEqualToString:@"photoset"]) {
        [self.picdelegate pushToPicDetail:model.skipID andTitle:model.title];
    } else if ([model.skipType isEqualToString:@"special"]) {
        [self.specialdelegate pushToSpecialDetail:model.skipID andTitle:model.title];
    } else {
        [self.delegate pushToDetail:model.url];
    }


    
}

- (void)setNewsArr:(NSMutableArray *)newsArr {
    if (_newsArr != newsArr) {
        _newsArr = newsArr;
    }
    [self.tableView reloadData];
}


- (void)createScrollView {
    

    self.scrollView = [[LCZBaseScrollView alloc] init];
//    self.scrollView.backgroundColor = [UIColor grayColor];
    [self.headView addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.equalTo(self.headView).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
        
    }];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    
    self.scrollView.delegate = self;
}

- (void)createTableView {
//    self.tableView = [[LCZBaseTableView alloc] initWithFrame:CGRectMake(0, 31, self.contentView.frame.size.width, self.contentView.frame.size.height) style:UITableViewStylePlain];
    self.tableView = [[LCZBaseTableView alloc] init];
    
    [self.contentView addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
       
//        make.top.equalTo(self.contentView).with.offset(31);
        make.edges.equalTo(self.contentView).with.insets(UIEdgeInsetsMake(0, 0, 64, 0));
        
    }];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
//    self.tableView.rowHeight = 110; // 110, 80
    
}

- (void)createHeadView {
    self.headView = [[LCZBaseView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT / 3)];
    self.tableView.tableHeaderView = self.headView;
//    self.headView.backgroundColor = [UIColor blueColor];
    
    
}

#pragma mark -- tableView协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.newsArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([[self.newsArr[indexPath.row] skipType] isEqualToString:@"photoset"]) {
    
        static NSString *reuse = @"photoset";
        LCZPhotosetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[LCZPhotosetTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
        }

        cell.titleLabel.text = [self.newsArr[indexPath.row] title];
        cell.replyCountLabel.text = [NSString stringWithFormat:@"%@跟帖", [self.newsArr[indexPath.row] replyCount]];
        [cell.leftImg sd_setImageWithURL:[NSURL URLWithString:[self.newsArr[indexPath.row] imgsrc]] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"]];
        [cell.midImg sd_setImageWithURL:[NSURL URLWithString:[self.newsArr[indexPath.row] imgextra][0][@"imgsrc"]] placeholderImage:[UIImage imageNamed:@"placehodler.jpg"]];
        [cell.rightImg sd_setImageWithURL:[NSURL URLWithString:[self.newsArr[indexPath.row] imgextra][1][@"imgsrc"]] placeholderImage:[UIImage imageNamed:@"placehodler.jpg"]];

        cell.nightBackgroundColor = [UIColor blackColor];
        cell.titleLabel.nightTextColor = [UIColor grayColor];
        return cell;
    } else if ([[self.newsArr[indexPath.row] skipType] isEqualToString:@"special"]) {
//    if ([[self.newsArr[indexPath.row] skipType] isEqualToString:@"special"]) {
        
        static NSString *reuse = @"special";
        LCZSpecialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[LCZSpecialTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
        }
        
        cell.titleLabel.text = [self.newsArr[indexPath.row] title];
        cell.digestLabel.text = [self.newsArr[indexPath.row] digest];
        [cell.imgsrcView sd_setImageWithURL:[NSURL URLWithString:[self.newsArr[indexPath.row] imgsrc]] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"]];
        
        cell.nightBackgroundColor = [UIColor blackColor];
        cell.titleLabel.nightTextColor = [UIColor grayColor];
        
        return cell;
    } else {
        
        static NSString *reuse = @"normal";
        LCZNormalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[LCZNormalTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
        }
        
        cell.titleLabel.text = [self.newsArr[indexPath.row] title];
        cell.digestLabel.text = [self.newsArr[indexPath.row] digest];
        [cell.imgsrcView sd_setImageWithURL:[NSURL URLWithString:[self.newsArr[indexPath.row] imgsrc]] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"]];
        cell.replyCountLabel.text = [NSString stringWithFormat:@"%@跟帖", [self.newsArr[indexPath.row] replyCount]];
        
        cell.nightBackgroundColor = [UIColor blackColor];
        cell.titleLabel.nightTextColor = [UIColor grayColor];
        
        return cell;
    }
    
    return nil;
    

    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.newsArr.count) {
        NSDateFormatter *f = [[NSDateFormatter alloc] init];
        f.dateFormat = @"dd";
        //      date -> string
        NSString *day = [f stringFromDate:[NSDate date]];
        f.dateFormat = @"MM";
        NSString *month = [f stringFromDate:[NSDate date]];
        LCZNewsModel *model = self.newsArr[indexPath.row];
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setObject:day forKey:@"day"];
        [dic setObject:month forKey:@"month"];
        [dic setObject:model.title forKey:@"myTitle"];
        [LCZCacheSaveTool calendarHistroySave:dic :@"Calendar/calendar.plist"];
        NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"Calendar/calendar.plist"];
        NSLog(@"%@", docPath);
        //    反归档
        NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
        NSLog(@"哈哈哈哈哈%@", array);
        if ([model.skipType isEqualToString:@"photoset"]) {
            NSLog(@"photoset");
            [self.picdelegate pushToPicDetail:model.skipID andTitle:model.title];
        } else if ([model.skipType isEqualToString:@"special"]) {
            NSLog(@"special");
            [self.specialdelegate pushToSpecialDetail:model.skipID andTitle:model.title];
        } else {
            NSLog(@"normal");
            [self.delegate pushToDetail:model.url];
        }

    }
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([[self.newsArr[indexPath.row] skipType] isEqualToString:@"photoset"]) {
        return 120;
    } else {
        return 80;
    }
}

#pragma mark -- scrollView的协议方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.scrollView) {
        if (scrollView.contentOffset.x == (pCOUNT + 1) * SCREENWIDTH ){
            scrollView.contentOffset = CGPointMake(SCREENWIDTH, 0);
        } if (scrollView.contentOffset.x == 0) {
            scrollView.contentOffset = CGPointMake(pCOUNT * SCREENWIDTH, 0);
        }
    }

}


@end
